package com.shop2cn.iapi.subscription.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.shop2cn.iapi.subscription.parameter.GetMchChargeConfigBean;
import com.shop2cn.iapi.subscription.service.GetMchChargeConfigCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
/**
*查询商家功能付费白名单Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetMchChargeConfig{
	private static GetMchChargeConfigBean getmchchargeconfigBean;
	private static GetMchChargeConfigCall getmchchargeconfigCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查询商家功能付费白名单Test");
	}

	@Before
	public void caseUp() {
		getmchchargeconfigBean=new GetMchChargeConfigBean();
		getmchchargeconfigCall=new GetMchChargeConfigCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getmchchargeconfigCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchChargeConfig_001() {
		Logger.start(true,"不在白名单用户查询，成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			getmchchargeconfigBean.setMchId(mchid);
			getmchchargeconfigCall.setData(getmchchargeconfigBean);
			getmchchargeconfigCall.callService();			
			Logger.verifyEquals("200", getmchchargeconfigCall.getString("code"), "验证code");
			Logger.verifyEquals(mchid,getmchchargeconfigCall.getMchid(), "验证mchid");
			equealsJson(null);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchChargeConfig_002() {
		Logger.start(true,"在白名单用户查询，成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchManageCallService.SetTrueMchChargeConfig(mchid, Arrays.asList(0,1,2,3,4),true);
			getmchchargeconfigBean.setMchId(mchid);
			getmchchargeconfigCall.setData(getmchchargeconfigBean);
			getmchchargeconfigCall.callService();			
			Logger.verifyEquals("200", getmchchargeconfigCall.getString("code"), "验证code");
			Logger.verifyEquals(mchid,getmchchargeconfigCall.getMchid(), "验证mchid");
			equealsJson(Arrays.asList(0,1,2,3,4));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
  	@Category({P1.class})
	@TestCase
	public void Tc_GetMchChargeConfig_003() {
		Logger.start(true,"已经去除白名单进行查询，成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchManageCallService.SetTrueMchChargeConfig(mchid, Arrays.asList(0,1,2,3,4),true);
			Thread.sleep(1000);
			GodmchManageCallService.SetTrueMchChargeConfig(mchid, Arrays.asList(0,2,4),false);
			Thread.sleep(1000);
			getmchchargeconfigBean.setMchId(mchid);
			getmchchargeconfigCall.setData(getmchchargeconfigBean);
			getmchchargeconfigCall.callService();
			Logger.verifyEquals("200", getmchchargeconfigCall.getString("code"), "验证code");
			Logger.verifyEquals(mchid,getmchchargeconfigCall.getMchid(), "验证mchid");
			equealsJson(Arrays.asList(1,3));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetMchChargeConfig_004() {
		Logger.start(true,"在白名单用户查询-自营商品套餐，成功");
		try {
			Integer mchid = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			GodmchManageCallService.SetTrueMchChargeConfig(mchid, Arrays.asList(6),true);
			getmchchargeconfigBean.setMchId(mchid);
			getmchchargeconfigCall.setData(getmchchargeconfigBean);
			getmchchargeconfigCall.callService();			
			Logger.verifyEquals("200", getmchchargeconfigCall.getString("code"), "验证code");
			Logger.verifyEquals(mchid,getmchchargeconfigCall.getMchid(), "验证mchid");
			equealsJson(Arrays.asList(6));
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	private void equealsJson(List<Integer> list){
		getmchchargeconfigCall.getconfig().forEach(b->{
			JsonObject mchCharge = b.getAsJsonObject();
			int funcTypeId = mchCharge.get("funcTypeId").getAsInt();
			boolean charge = mchCharge.get("charge").getAsBoolean();
			if(list==null){
				Logger.verifyEquals(false,charge, "验证"+funcTypeId+"未被订购");
			}else {
				if(list.contains(funcTypeId)){
					Logger.verifyEquals(true,charge, "验证"+funcTypeId+"已经被订购");
				}
			}
			
		});
	}
}